上傳檔案到 NodeMCU (Upload files to NodeMCU)

需先安裝 ampy ( Adafruit MicroPython Tool )

pip install adafruit-ampy

https://github.com/adafruit/ampy


In [1]:
import os

設定COM port (set current COM port)


In [14]:
com_port = 'COM12'
# com_port = 'COM13'
com_port = 'COM15'
# com_port = 'COM16'

列出檔案 (list files)


In [ ]:
# 現存檔案
files = !ampy --port {com_port} ls
files

刪除檔案 (delete all files)


In [ ]:
# 清空
for file in files:
    print('Deleting {0}'.format(file))
    !ampy --port {com_port} rm {file}

Functions for copying files


In [3]:
def copy_one_file(folder, file):         
    print('Copying {0}'.format(file))
    !ampy --port {com_port} put {os.path.join(folder, file)}
    
    
def copy_all_files(folders, main_filename = 'main.py'):
    files = !ampy --port {com_port} ls

    if main_filename in files:
        print('Deleting {0}'.format(main_filename))
        !ampy --port {com_port} rm {main_filename}

    for folder in folders: 
        for file in os.listdir(folder):
            if not file.startswith('_') and not file.startswith(main_filename):
                print('Copying {0}'.format(file))
                !ampy --port {com_port} put {os.path.join(folder, file)}

Copy 檔案到開發板 (copy files onto ESP8266 / NodeMCU, all needed fils will be put in the same folder)


In [9]:
folders = ['..\\codes\\micropython', '..\\codes\\node', '..\\codes\\shared']
main_filename = 'main.py'

copy_all_files(folders, main_filename)
            
copy_one_file('..\\codes\\micropython', main_filename)


Deleting main.py
Copying boot.py
Copying hardware.py
Copying led.py
Copying u_python.py
Copying u_python_profiler.py
Copying watchdog.py
Copying webrepl_cfg.py
Copying asynch_result.py
Copying node.py
Copying queue_manager.py
Copying socket_client.py
Copying worker.py
Copying worker_config.py
Copying worker_cpython.py
Copying worker_neuron.py
Copying worker_upython.py
Copying commander.py
Copying config.py
Copying data_transceiver.py
Copying main.py

單一檔案上傳 (single file upload, in case needed)


In [ ]:
copy_one_file('..\\..\\dmz', 'config.py')

In [7]:
copy_one_file('..\\codes\\shared', 'config.py')


Copying config.py

In [16]:
copy_one_file('..\\codes\\node', 'node.py')


Copying node.py

In [15]:
copy_one_file('..\\codes\\micropython', 'u_python.py')


Copying u_python.py

列出檔案 (list files)


In [ ]:
# !ampy --port {com_port} ls

檢查檔案內容 (check file content)


In [ ]:
# !ampy --port {com_port} get boot.py

In [ ]:
# !ampy --port {com_port} get main.py

連網測試 (network config and test)


In [ ]:
# 連上網路
# import network; nic=network.WLAN(network.STA_IF); nic.active(True); nic.connect('SSID','password');nic.ifconfig()
# import network; nic=network.WLAN(network.STA_IF); nic.active(True); nic.connect('Kingnet-70M-$370', '');nic.ifconfig()
# import network; nic=network.WLAN(network.STA_IF);nic.ifconfig();nic.config('mac');nic.ifconfig((['mac',])

In [ ]:
# 發出 http request
# import socket;addr=socket.getaddrinfo('micropython.org',80)[0][-1]
# s = socket.socket();s.connect(addr);s.send(b'GET / HTTP/1.1\r\nHost: micropython.org\r\n\r\n');data = s.recv(1000);s.close()

In [ ]:
# Delete all files
# import u_python;u_python.del_all_files();import os;os.listdir()

Run Broker container on Raspberry Pi

copy folder 'codes' to Raspberry Pi under folder '/data/elastic_network_of_things_with_micropython',
so Raspberry Pi has folder '/data/elastic_network_of_things_with_micropython/codes'
then run the command below on Raspberry Pi.

docker run -it -p 9662:9662 --name=Broker --hostname=Broker --volume=/data/elastic_network_of_things_with_micropython:/project wei1234c/python_armv7 /bin/sh -c "cd /project/codes/broker && python3 broker.py"